home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-08 | 8.0 KB | 298 lines | [TEXT/MPCC] |
- // ----------------------------------------------------------------------------------
- // TEHanlder.c
- // ----------------------------------------------------------------------------------
- // Handles most of the important routines related to the text window's TE.
- //
- // ----------------------------------------------------------------------------------
- // History:
- // 9/20/94 CG Original
- // 10/4/94 CG Tried to fix the scroll to text sync after a paste or TEKey.
- // It still needs some fine tuning.
-
- #include "TEHandler.h"
- #include "WindowHandler.h"
- #include "Globals.h"
-
- void SetView(WindowPtr theWindow);
- void AdjustText(TEHandle TEH);
- void SetScrollBar();
- void DoTEKey(WindowPtr theWindow, char theChar);
- void DoControl(WindowPtr theWindow, ControlHandle theControl, short partCode, Point where);
- pascal void TrackScrollBar(ControlHandle theControl, short partCode);
- Boolean TrackContentClick(void);
- void SizeScroll(ControlHandle theControl, WindowPtr theWindow);
-
- short gPageLines;
-
- // ----------------------------------------------------------------------------------
- // ----------------------------------------------------------------------------------
- // DoControl
- // ----------------------------------------------------------------------------------
- // Handle a mouse click in a scroll bar. Right now auto scrolling with the thumb
- // isn't implemented.
-
- void DoControl( WindowPtr theWindow, ControlHandle theControl,
- short partCode, Point where )
- {
- ControlActionUPP theAction;
- short oldV;
- short newV;
-
- gDocWindow = theWindow; // so our control handler can get window data
-
- switch ( partCode )
- {
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- theAction = NewControlActionProc((ProcPtr) TrackScrollBar);
- TrackControl( theControl, where, theAction );
- break;
- case inThumb:
- oldV = GetCtlValue( theControl );
- TrackControl( theControl, where, (ControlActionUPP) nil ); // add later
- SetScrollBar();
- break;
- } // switch
-
- return;
-
- } // DoControl
-
-
- // ----------------------------------------------------------------------------------
- // TrackScrollBar
- // ----------------------------------------------------------------------------------
- // This is the callback routine that handles tracking the scrollbar. This is for
- // when you continue to hold the mouse down in a scrollbar and want it to continue
- // scrolling.
-
- pascal void TrackScrollBar( ControlHandle theControl, short partCode )
- {
- int pageSize;
- int amount;
- int oldval;
-
- WinDocRecord **theData;
- TEHandle theTE;
-
- theData = (WinDocRecord**) GetWRefCon(gDocWindow);
- theTE = (**theData).DocTE;
-
- pageSize = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight - 1;
-
- switch ( partCode ){
- case inUpButton:
- amount = -1;
- break;
- case inDownButton:
- amount = 1;
- break;
- case inPageUp:
- amount = - pageSize;
- break;
-
- case inPageDown:
- amount = pageSize;
- break;
-
- } // switch
-
- oldval = GetCtlValue(theControl);
- SetCtlValue(theControl, oldval + amount);
- SetScrollBar();
-
- return;
-
- } // TrackScrollBar
-
-
-
- // ----------------------------------------------------------------------------------
- // TrackContentClick
- // ----------------------------------------------------------------------------------
- // Handles clicks in the window.
-
- Boolean TrackContentClick( void )
- {
- Point where;
- Rect viewRect;
- TEHandle theTE;
- ControlHandle theControl;
- GrafPtr thePort;
- RgnHandle oldClip;
- Rect scrollRect;
- Boolean needRedraw;
-
- GetPort( &thePort );
- SetPort( gDocWindow );
-
- GetMouse( &where );
-
- theTE = (TEHandle)GetWRefCon( gDocWindow );
-
- viewRect = (*theTE)->viewRect;
-
- theControl = ((WindowPeek)gDocWindow)->controlList; /* We know there's 1 control */
-
- needRedraw = false;
-
- // If the mouse is above or below the window, we fake a mouse click on the scroll bar
- if ( where.v < viewRect.top )
- { TrackScrollBar( theControl, inUpButton );
- needRedraw = true;
- } else if ( where.v > viewRect.bottom )
- { TrackScrollBar( theControl, inDownButton );
- needRedraw = true;
- }
-
-
- // redraw the window
-
- if ( needRedraw )
- { oldClip = NewRgn();
- if ( oldClip )
- { GetClip( oldClip );
- scrollRect = (*theControl)->contrlRect;
- ClipRect( &scrollRect );
- Draw1Control( theControl );
- SetClip( oldClip );
- DisposeRgn( oldClip );
- } // if
- } // if
-
- SetPort( thePort );
-
- return true;
-
- } // TrackContentClick
-
- // ----------------------------------------------------------------------------------
- // SizeScroll
- // ----------------------------------------------------------------------------------
- // Handles resizing the control in response to growWindow.
-
- void SizeScroll(ControlHandle theControl, WindowPtr theWindow)
- {
- MoveControl( theControl, theWindow->portRect.right - 15, -1 );
- SizeControl( theControl, 16, theWindow->portRect.bottom - theWindow->portRect.top - 13 );
-
- return;
- } // SizeScroll
-
-
- // ----------------------------------------------------------------------------------
- // TEAdjustView
- // ----------------------------------------------------------------------------------
- // This basically adjusts the view so that the scrollbars will fit and to adjust the
- // size of the TEView when the window's size changes.
-
- void TEAdjustView(WindowPtr theWindow, TEHandle theTE)
- {
- Rect TERect;
- short offset;
- Rect WindowRect;
-
- WindowRect = theWindow->portRect;
-
- TERect.left = WindowRect.left;
- TERect.right = WindowRect.right - 15;
- TERect.bottom = WindowRect.bottom;
- TERect.top = WindowRect.top;
-
- InsetRect(&TERect, 4, 4);
-
- (*theTE)->viewRect = TERect;
- (*theTE)->destRect = TERect;
-
- TECalText(theTE);
-
- } // TEAdjustView
-
-
- void ShowSelect(TEHandle theTE, ControlHandle theControl)
- {
- register int n, topLine, bottomLine, linesInScreen;
-
- linesInScreen = ((**theTE).viewRect.bottom - (**theTE).viewRect.top)/(**theTE).lineHeight;
-
- n = (**theTE).nLines - linesInScreen;
-
- // correct for ending CR bug in TE
- if ( (**theTE).teLength > 0 && (*((**theTE).hText))[(**theTE).teLength - 1] == '\r')
- n++;
-
- SetCtlMax(theControl, n > 0 ? n : 0);
-
- topLine = GetCtlValue(theControl);
- bottomLine = topLine + linesInScreen;
-
- if ((**theTE).selStart < (**theTE).lineStarts[topLine] ||
- (**theTE).selStart >= (**theTE).lineStarts[bottomLine])
- { for (n = 0; (**theTE).selStart >= (**theTE).lineStarts[n]; n++)
- ;
- SetCtlValue(theControl, n - linesInScreen/2);
- }
- SetScrollBar();
- }
-
-
-
- // ----------------------------------------------------------------------------------
- // SetScrollBar
- // ----------------------------------------------------------------------------------
- // This is a tricky routine that doesn't work terribly well right now. Basically
- // it attempts to sync the scrollbars with the text. It further tries to keep
- // the insertion point in a reasonable place in the window for typing.
- // Unfortunately this leads to some annoying 'jumps' in the text at times. I
- // really need to just sit down and fiddle with this. Right now it is basically
- // a collection of various hacks found in editors from the net.
- //
- // This, along with menu feedback, are the two main things I need to fix in the
- // editor.
-
- void SetScrollBar()
- {
- int oldScroll, newScroll, delta;
-
-
-
- ControlHandle theControl;
- WinDocRecord **theData;
- TEHandle theTE;
-
- theData = (WinDocRecord**) GetWRefCon(gDocWindow);
- theControl = (*theData)->DocVScroll;
- theTE = (*theData)->DocTE;
- oldScroll = (**theTE).viewRect.top - (**theTE).destRect.top;
- newScroll = GetCtlValue(theControl) * (**theTE).lineHeight;
- delta = oldScroll - newScroll;
-
- if (delta != 0)
- TEScroll(0, delta, theTE);
-
- } // SetScrollBar
-
-
-
- // ----------------------------------------------------------------------------------
- // DoTEKey
- // ----------------------------------------------------------------------------------
- // Handles a keypress. It sends the character to the TE library.
-
- void DoTEKey(WindowPtr theWindow, char theChar)
- { TEHandle theTE;
- WinDocRecord **theData;
-
- theData = (WinDocRecord**) GetWRefCon(theWindow);
- theTE = (**theData).DocTE;
-
- TEKey(theChar, theTE);
- ObscureCursor();
-
- ShowSelect(theTE, (**theData).DocVScroll);
- (**theData).dirty = TRUE;
-
- } // DoTEKey
-